Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
Angular ngStyle Parser Error: Unexpected token [, expected identifier or keyword

I need to be able to pass a dynamic string using the fieldName in order to get an attribute within the item object. In order to make it easy to duplicate the issue I have given a small example. The problem is this syntax works properly with angular 12.2.5 as I checked but did not work with angular 12.0.0 or below. I can simply call a function but I know this is not good for performance. Is there an alternate way to write this syntax for older angular applications?

HTML

<p [style.background]="item?.[fieldName]">
  Some Text
</p>

ts

item =  { warn: 'yellow', success: 'green', error: 'red' };
fieldName = 'success';

Error looks like this

Parser Error: Unexpected token [, expected identifier or keyword at column 20 in [{background: item?.[fieldName]}]
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try to use ternary operator ?::

<p [style.background]="item[fieldName] ? item[fieldName] : ''">
  Some Text
</p>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda